package com.alibaba.citrus.cr.mn.order.center.facade.function.common;

import com.alibaba.citrus.cr.mn.order.center.common.util.AssertUtils;
import com.alibaba.citrus.cr.mn.order.center.facade.function.check.CommonCheck;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.mnordercenter.model.dto.Modifiers;
import com.epoch.app.mnordercenter.model.dto.OrderAtomBatchCloseRequest;
import com.epoch.app.mnordercenter.model.dto.OrderBizBatchCloseRequest;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomCloseRequest;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomCloseResponse;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhangxiaorui
 * @ClassName SalesOrderBizCreateServiceImpl
 * @description: TODO
 * @date 2023/05/16 14:04
 * @version: 1.0
 */
public abstract class AbstractOrderBizCloseService implements OrderBizCloseService<OrderBizBatchCloseRequest, OrderAtomBatchCloseRequest, OrderAtomCloseResponse> {
    private static final Log log = Log.getLogger(AbstractOrderBizCloseService.class);

    @Resource
    private CommonCheck commonCheck;

    @Override
    public void paramsInvalidCheck(OrderBizBatchCloseRequest orderBizBatchCloseRequest) {
        AssertUtils.isTrue(CollectionUtils.isEmpty(orderBizBatchCloseRequest.getOrderList()), "OC-01-001-01-15-001", "单据列表");

        orderBizBatchCloseRequest.getOrderList().forEach(o -> {
            AssertUtils.isTrue(StringUtils.isBlank(o.getModifierCode()), "OC-01-001-01-15-001", "修改人工号");
            AssertUtils.lengthCheck(o.getModifierCode(), 50, false, "OC-01-001-01-15-004", "修改人工号");
        });
    }

    @Override
    public void preHandleRequestParams(OrderBizBatchCloseRequest orderBizBatchCloseRequest) {
        orderBizBatchCloseRequest.setOrderList(Optional.ofNullable(orderBizBatchCloseRequest.getOrderList()).orElse(Lists.newArrayList()));
        for (OrderAtomCloseRequest salesOrder : orderBizBatchCloseRequest.getOrderList()) {
            if (StringUtils.isNotBlank(orderBizBatchCloseRequest.getBusinessUnitCode())) {
                salesOrder.setBusinessUnitCode(orderBizBatchCloseRequest.getBusinessUnitCode());
            }
            if (StringUtils.isNotBlank(orderBizBatchCloseRequest.getBusinessType())) {
                salesOrder.setBusinessType(orderBizBatchCloseRequest.getBusinessType());
            }
            if (StringUtils.isNotBlank(orderBizBatchCloseRequest.getFromSys())) {
                salesOrder.setFromSys(orderBizBatchCloseRequest.getFromSys());
            }
            if (StringUtils.isNotBlank(orderBizBatchCloseRequest.getModifierCode())) {
                salesOrder.setModifierCode(orderBizBatchCloseRequest.getModifierCode());
            }
        }
    }

    @Override
    public List<OrderAtomCloseResponse> assembleBizBaseInfo(OrderBizBatchCloseRequest orderBizBatchCloseRequest) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("assembleBizBaseInfo");
        List<String> modifierCode = orderBizBatchCloseRequest.getOrderList().stream()
                .map(OrderAtomCloseRequest::getModifierCode)
                .distinct().collect(Collectors.toList());
        Map<String, Modifiers> modifiers = commonCheck.getModifiers(modifierCode);
        Iterator<OrderAtomCloseRequest> iterator = orderBizBatchCloseRequest.getOrderList().iterator();
        List<OrderAtomCloseResponse> respList = new ArrayList<>();
        while (iterator.hasNext()) {
            OrderAtomCloseRequest orderAtomCloseRequest = iterator.next();
            OrderAtomCloseResponse response = new OrderAtomCloseResponse();
            response.setSalesOrderNo(orderAtomCloseRequest.getSalesOrderNo());
            response.setLineNum(orderAtomCloseRequest.getLineNum());
            response.setSuccess(Boolean.FALSE);

            Modifiers modifier = modifiers.get(orderAtomCloseRequest.getModifierCode());
            if (Objects.isNull(modifier) || modifier.getStatus() != 1) {
                response.setMessage("人员不存在");
                respList.add(response);
                iterator.remove();
                continue;
            }

            orderAtomCloseRequest.setModifierName(modifier.getName());
        }
        stopWatch.stop();
        log.info("AbstractOrderBizCloseService#assembleBizBaseInfo stopWatch.getTotalTimeSeconds()={}", stopWatch.getTotalTimeSeconds());

        return respList;
    }

    @Override
    public OrderAtomBatchCloseRequest convertAtomRequest(OrderBizBatchCloseRequest orderBizBatchCloseRequest) {
        return orderBizBatchCloseRequest;
    }

    @Override
    public Result<List<OrderAtomCloseResponse>> processAtomAbility(OrderAtomBatchCloseRequest orderAtomBatchCloseRequest, List<OrderAtomCloseResponse> responses) {
        return Result.success(responses, "", "");
    }

}
